package com.globalwave.system.privilege
{
    import com.globalwave.common.event.RecordEvent;
    import com.globalwave.util.HttpServiceUtil;
    
    import flash.display.DisplayObject;
    
    import mx.collections.ArrayCollection;
    import mx.controls.Alert;
    import mx.core.Application;
    import mx.events.CloseEvent;
    import mx.managers.PopUpManager;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.http.HTTPService;
  
    public class F {
        public function F(displayObject : DisplayObject) {
            this.displayObject = displayObject;
        }
        
        private var deleteObjects : Array ;
        private var displayObject : DisplayObject ;
               
        public function openCreateWindow(parentObject : Object):void {
            if (parentObject == null) {
                Alert.show("请选择父权限！") ;
                return ;
            }
            
            var app : DisplayObject = DisplayObject(Application.application) ;
            var editWindow : PrivilegeEditWindow = 
                        PrivilegeEditWindow(PopUpManager.createPopUp(app, PrivilegeEditWindow, true));
            
            editWindow.parentObject = parentObject ;
            
            editWindow.addEventListener(RecordEvent.ADD_SUCCESS,dispatchRecordEvent);
        }        
            
        public function openUpdateWindow(updateObject : Object):void {
            if (updateObject == null) {
                Alert.show("请选择要编辑的权限！") ;
                return ;
            }
            if (updateObject.@privilege_id == "") {
                Alert.show("根结点不能编辑！") ;
                return ;
            }
            
            var app : DisplayObject = DisplayObject(Application.application) ;
            var editWindow : PrivilegeEditWindow = 
                        PrivilegeEditWindow(PopUpManager.createPopUp(app, PrivilegeEditWindow, true));
            
            editWindow.updateObject = updateObject ;
            editWindow.parentObject = updateObject.parent() ;
                            
            editWindow.addEventListener(RecordEvent.UPDATE_SUCCESS,dispatchRecordEvent);
        }
        
        
        
        public function deleteRecord(deleteObjects: Array):void {
            if (deleteObjects == null) {
                Alert.show( "请选择记录！" );
                return ;
            }
             
            for (var i : int = deleteObjects.length - 1 ; i >= 0 ; i --) {   
                if (deleteObjects[i].children().length() != 0) {
                    Alert.show( "必先删除所有子记录！" );
                    return ;
                }
            }
            
            this.deleteObjects = deleteObjects ;
               
            var msg : String ;
            if (deleteObjects == null || deleteObjects.length == 1) {
                msg = "是否删除[" + deleteObjects[0].@name_ + "]？" ;
            } else {
                msg = "是否删除所有选中记录？" ;
            }
            Alert.show( msg , "删除确认", Alert.YES|Alert.NO, null, deleteRecordConfirm);                
        }
    
    
        
        private function deleteRecordConfirm(event:CloseEvent):void {
            if (event.detail==Alert.YES) {
                var params :Object = new Object();
                                      
                params["012345678"] = "012345678" ;
                var ids : ArrayCollection = new ArrayCollection() ;
                for (var i : int = 0 ; i < this.deleteObjects.length ; i ++) {
                    ids.addItem(deleteObjects[i].@privilege_id) ;
                }
            
                params["privilege_ids"] = ids.toArray() ;
                                              
                var httpService : HTTPService = HttpServiceUtil.getService("/system/privilege_delete");
                
                httpService.addEventListener(ResultEvent.RESULT,deleteRecordCompleted) ;
        
                httpService.send(params);     
        
            }            
        }        
          
        private function deleteRecordCompleted(event:ResultEvent) : void {
            var result : XML = new XML(event.result) ;
            if(result.elements("code") != "0") {
                return ;
            }
            displayObject.dispatchEvent(new RecordEvent(RecordEvent.DELETE_SUCCESS)) ;
        
        }
        
        public function dispatchRecordEvent(event:RecordEvent) : void {
            displayObject.dispatchEvent(event.copy()) ;
        }
    
 
    }
}